/*
 * Copyright 2018-present KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kunminx.architecture.utils;

/**
 * Create by KunMinX at 19/7/20
 */

public class DisplayUtils {

  /**
   * convert px to its equivalent dp
   * <p>
   * 将px转换为与之相等的dp
   */
  public static int px2dp(float pxValue) {
    final float scale = Utils.getApp().getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
  }

  /**
   * convert dp to its equivalent px
   * <p>
   * 将dp转换为与之相等的px
   */
  public static int dp2px(float dipValue) {
    final float scale = Utils.getApp().getResources().getDisplayMetrics().density;
    return (int) (dipValue * scale + 0.5f);
  }

  /**
   * convert px to its equivalent sp
   * <p>
   * 将px转换为sp
   */
  public static int px2sp(float pxValue) {
    final float fontScale = Utils.getApp().getResources().getDisplayMetrics().scaledDensity;
    return (int) (pxValue / fontScale + 0.5f);
  }

  /**
   * convert sp to its equivalent px
   * <p>
   * 将sp转换为px
   */
  public static int sp2px(float spValue) {
    final float fontScale = Utils.getApp().getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue * fontScale + 0.5f);
  }
}
